package mysql

import "gorm.io/gorm"

type User struct {
	gorm.Model
	Name   string `gorm:"type:varchar(20)" json:"name"`
	Mobile string `gorm:"type:char(11)" json:"mobile"`
	Sign   string `gorm:"type:varchar(50)" json:"sign"`
	Sex    int64  `gorm:"type:int(20)" json:"sex"`
	Age    int64  `gorm:"type:int(10)" json:"age"`
}

func (u *User) FindUserByMobile(mobile string) (*User, error) {
	err := DB.Model(&u).Where("mobile=?", mobile).Find(&u).Error
	if err != nil {
		return nil, err
	}
	return u, nil
}

func (u *User) CreateUser(mobile string) (*User, error) {
	user := &User{
		Mobile: mobile,
	}
	err := DB.Create(&user).Error
	if err != nil {
		return nil, err
	}
	return user, nil
}
func (u *User) FindUserBy(userId int64) (user []*User, err error) {
	err = DB.Where("id=?", userId).Find(&user).Error
	if err != nil {
		return nil, err
	}

	return user, nil
}
